package top.tagao.yygh.cmn.controller;

import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.tagao.yygh.cmn.listener.DictListener;
import top.tagao.yygh.cmn.service.DictService;
import top.tagao.yygh.common.result.Result;
import top.tagao.yygh.model.cmn.Dict;
import top.tagao.yygh.vo.cmn.DictEeVo;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static org.reflections.Reflections.log;

/**
 * @author fu-xiao-liu
 * @Date 2022/3/1 14:32
 */
@Api(tags = "数据字典接口")
//@CrossOrigin
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {
    @Autowired
    private DictService dictService;


    @ApiOperation(value = "导出数据字典接口")
    @GetMapping("exportData")
    public void exportDict(HttpServletResponse response){
        dictService.exportDict(response);
    }


    @ApiOperation(value = "导入数据字典接口")
    @PostMapping("importData")
    @CacheEvict(value = "dict", allEntries=true)
    public Result importDict(MultipartFile file){
        log.info("上传文件");

        try {
            EasyExcel.read(file.getInputStream(), DictEeVo.class,new DictListener(dictService)).sheet().doRead(); //或者是用dictMapper
        } catch (IOException e) {
            e.printStackTrace();
        }

        return Result.ok();
    }

    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result<List<Dict>> findByDictCode(
            @ApiParam(name = "dictCode", value = "节点编码", required = true)
            @PathVariable String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }

    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    @ResponseBody
    public Result findChildData(@PathVariable Long id) {
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }

    @ApiOperation(value = "获取数据字典名称")
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@ApiParam(name = "dictCode", value = "上级编码", required = true)
                              @PathVariable("dictCode") String dictCode,
                          @ApiParam(name = "value", value = "值", required = true)
                          @PathVariable("value") String value) {
        String dictName = dictService.getDictName(dictCode,value);
        return dictName;
    }

    @ApiOperation(value = "获取数据字典名称")
    @GetMapping("getName/{value}")
    public String getName(@ApiParam(name = "value", value = "值", required = true)
                          @PathVariable("value") String value) {
        String dictName = dictService.getDictName("",value);
        return dictName;
    }
}
